package cn.com.b2office.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.com.b2office.dao.ArticleDao;
import cn.com.b2office.domain.Article;
import cn.com.b2office.domain.ArticleCategory;

public class ArticleDaoHibernateImpl extends HibernateDaoSupport implements
		ArticleDao {

	private static final Log logger = LogFactory
			.getLog(ArticleDaoHibernateImpl.class);

	public void addArticleHits(int articleId, int hits) {
		Article article = (Article) getHibernateTemplate().get(Article.class,
				articleId);
		if (article == null)
			return;
		article.setClicks(hits);
		getHibernateTemplate().update(article);
	}

	public int createArticle(Article article) {
		return (Integer) getHibernateTemplate().save(article);
	}

	public Article getArticle(int articleId) {
		return (Article) getHibernateTemplate().get(Article.class, articleId);
	}

	public ArticleCategory getArticleCategory(int id) {
		return (ArticleCategory) getHibernateTemplate().get(
				ArticleCategory.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Article> listArticle(int articleTypeId, int firstResult,
			int maxResults) {
		List<Article> list = null;
		Session s = getSession();
		try {
			String hql = "from Article where categoryId=:categoryId order by sortWeight desc,articleId desc";
			Query query = s.createQuery(hql).setInteger("categoryId",
					articleTypeId).setFirstResult(firstResult).setMaxResults(
					maxResults);
			// 查询缓存
			query.setCacheable(true);
			list = query.list();
		} catch (RuntimeException e) {
			logger.error("listArticle throw Exception:" + e.getMessage());
			throw e;
		} finally {
			releaseSession(s);
		}
		return list;
	}

	public void removeArticle(Article article) {
		getHibernateTemplate().delete(article);
	}

	public void removeArticle(int articleId) {
		Object article = getHibernateTemplate().load(Article.class, articleId);
		getHibernateTemplate().delete(article);
	}

	public void updateArticle(Article article) {
		getHibernateTemplate().update(article);

	}

	@SuppressWarnings("unchecked")
	public List<Article> getNextArticles(int curArticleId, int count) {
		List<Article> list = new ArrayList<Article>();
		Session s = getSession();
		try {
			Article article = (Article) s.load(Article.class, curArticleId);
			int categoryId = article.getCategoryId();
			String hql = "from Article where categoryId=:categoryId order by sortWeight desc,articleId desc";
			Query query = s.createQuery(hql).setInteger("categoryId",
					categoryId);
			// 查询缓存
			query.setCacheable(true);
			List<Article> allArticles = query.list();
			for (int i = 0; i < allArticles.size(); i++) {
				Article a = allArticles.get(i);
				if (a.getArticleId() == curArticleId) {
					int j = i + 1;
					for (; j < allArticles.size() && j <= (i + count); j++) {
						list.add(allArticles.get(j));
					}
					break;
				}
			}
		} catch (RuntimeException e) {
			logger.error("getNextArticles throw Exception:" + e.getMessage());
			throw e;
		} finally {
			releaseSession(s);
		}
		return list;
	}

	public List<Article> listAllArticle(int categoryId) {
		List<Article> list = null;
		Session s = getSession();
		try {
			String hql = "from Article where categoryId=:categoryId order by sortWeight desc,articleId desc";
			Query query = s.createQuery(hql).setInteger("categoryId",
					categoryId);
			// 查询缓存
			query.setCacheable(true);
			list = query.list();
		} catch (RuntimeException e) {
			logger.error("listArticle throw Exception:" + e.getMessage());
			throw e;
		} finally {
			releaseSession(s);
		}
		return list;
	}

	public List<ArticleCategory> listAllCategory(List<Integer> categoryIdList) {
		List<ArticleCategory> list = null;
		Session s = getSession();
		try {
			Criteria criteria = s.createCriteria(ArticleCategory.class);
			criteria.add(Expression.in("categoryId", categoryIdList));
			// 查询缓存
			criteria.setCacheable(true);
			list = criteria.list();
		} catch (RuntimeException e) {
			logger.error("listArticle throw Exception:" + e.getMessage());
			throw e;
		} finally {
			releaseSession(s);
		}
		return list;
	}

}
